package org.lgiavedoni.android.franq.core.place;

import java.util.Date;

import org.lgiavedoni.android.franq.core.calendar.CalendarEvent;
import org.lgiavedoni.android.franq.core.calendar.CalendarEventImp;
import org.lgiavedoni.android.franq.core.calendar.RegisterCalendarEvent;
import org.lgiavedoni.android.franq.core.sync.DaoSynObject;

public class Permanence extends DaoSynObject implements RegisterCalendarEvent {

	protected Date endDate;
	private final Place place;

	public Permanence(Place place) {
		this.place = place;
	}

	public void end() {
		endDate = new Date();
	}

	public long duration() {
		return endDate == null ? -1 : endDate.getTime() - getCreationDate().getTime();
	}

	public boolean isCompleted() {
		return endDate != null ? true : false;
	}

	public Place getPlace() {
		return place;
	}

	@Override
	public CalendarEvent toCalendarEvent() {
		if (isCompleted())
			return new CalendarEventImp("@" + place.getName(), "@" + place.getName(), getCreationDate(), endDate);
		else
			return new CalendarEventImp("@" + place.getName(), "@" + place.getName(), getCreationDate());
	}

	public Date getEndDate() {
		return endDate;
	}

}
